package com.ftg.midamall.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ftg.midamall.comm.vo.CodeStatusEnum;
import com.ftg.midamall.comm.vo.RespMessUtil;
import com.ftg.midamall.entity.PmsBrand;
import com.ftg.midamall.mapper.PmsBrandMapper;
import com.ftg.midamall.service.IPmsBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 品牌表 服务实现类
 * </p>
 *
 * @author qjw
 * @since 2023-11-02
 */
@Service
public class PmsBrandServiceImpl extends ServiceImpl<PmsBrandMapper, PmsBrand> implements IPmsBrandService {

    @Resource
    PmsBrandMapper pmsBrandMapper;

    @Override
    public RespMessUtil<IPage> showAllWithPage(long pageSize, long pageLimit, String name) {
        IPage<PmsBrand> ipage = new Page<>(pageSize, pageLimit);
        QueryWrapper<PmsBrand> query = new QueryWrapper<>();
        if (!StrUtil.isBlank(name)) {
            query.like("name", name);
        }
        return RespMessUtil.build(CodeStatusEnum.SUCCESS, this.getBaseMapper().selectPage(ipage, query));
    }

    @Override
    public int insertBrand(PmsBrand pmsBrand) {
        PmsBrand i = pmsBrandMapper.selectByName(pmsBrand.getName());
        if(!Objects.isNull(i)){
            throw new RuntimeException("该品牌名称已存在");
        }
        return this.getBaseMapper().insert(pmsBrand);
    }

    @Override
    public Integer deleById(Long id) {
        return this.getBaseMapper().deleteById(id);

    }

    public Integer deleteBatchByIds(List<Long> ids) {
        return this.getBaseMapper().deleteBatchIds(ids);
    }


    public int updateBrand(PmsBrand brand, Long id) {

        if(Objects.isNull(id)){
            throw new RuntimeException("该id不存在");
        }

        PmsBrand pmsBrand = pmsBrandMapper.selectByName(brand.getName());
        if(!Objects.isNull(pmsBrand)){
            throw new RuntimeException("该品牌名称已存在");
        }

        return pmsBrandMapper.updateById(brand, id);

    }
}
